<?php
/**
 * Created by PhpStorm.
 * User: linyida
 * Date: 2019/7/27
 * Time: 上午9:48
 */
namespace App\Storage;

use EasySwoole\Component\Singleton;
use EasySwoole\MysqliPool\Mysql;

/**
 * 商户的客户列表
 * Class ServiceRole
 * @package App\Storage
 */
class ServiceRole {

    use Singleton;
    protected $table = "ven_shop_service";

    function __construct()
    {
    }


    /**
     * 存储一条消息
     * 格式: ["shop_id","role_id","shop_user_id","status"]
     * @param $message
     * @return bool
     */
    public function save($message) {
        $db = Mysql::getInstance()->pool('mysql')->getObj();
        if ($db) {
            $db->insert($this->table,$message);
            Mysql::getInstance()->pool('mysql')->recycleObj($db);
        }

    }

    /**
     * 获取数据
     * @param $where ['k' => 'v'] 条件
     * @return array
     */
    public function read($where) {
        $db = Mysql::getInstance()->pool('mysql')->getObj();
        if ($db) {
            $db->where('status', 1, '=', 'and');
            if (is_array($where)) {
                foreach ($where as $key => $val) {
                    $db->where($key, $val, '=', 'and');
                }
            }
            $data = $db->get($this->table);
            Mysql::getInstance()->pool('mysql')->recycleObj($db);
            if ($data) {
                return $data;
            }
        }
        return [];
    }

    /**
     * 更新数据
     * @param $where
     * @param $data
     * @return mixed
     */
    public function update($where, $data) {
        $db = Mysql::getInstance()->pool('mysql')->getObj();
        if ($db) {
            $res = $db->where($where)->update($this->table, $data);
            Mysql::getInstance()->pool('mysql')->recycleObj($db);
            return $res;
        }
    }

}